import 'package:dio/dio.dart';
import 'package:logger/logger.dart';

import '../../../data/dio/api/bt_dio_network.dart';
import '../../../data/dio/api/bt_net_data_source.dart';
import '../../../data/dio/bt_api_result.dart';
import '../../../data/models/bt_user_info.dart';
import '../../../data/stats/bt_log_pages.dart';
import '../../../data/stats/bt_log_pv_manger.dart';
import '../../../part/bt_rc_im_engine.dart';
import '../../utils/bt_constants.dart';
import '../../utils/bt_event_bus.dart';
import '../utils/bt_constants.dart';

class BtMessageRepository {
  final logger = Logger();
  final BtNetDataSource network = BtDioNetwork();

  void addFriend(String followUserId, Function(bool success, String msg) onResult) {
    var map = <String, String>{};
    if (followUserId.isNotEmpty) {
      map['followUserId'] = followUserId;
    }

    network.addFriend(map).then((result) {
      onResult(result.isSuccess, result.isSuccess ? 'Follow successfully' : result.msg);
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }

  void unfriend(String followUserId, Function(bool success, String msg) onResult) {
    var map = <String, String>{};
    if (followUserId.isNotEmpty) {
      map['followUserId'] = followUserId;
    }

    network.unfriend(map).then((result) {
      if (result.isSuccess) {
        eventBus.fire(BtEventFn(freshFollowedList));
      }
      onResult(result.isSuccess, result.isSuccess ? 'Success' : result.msg);
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }

  void reportOrBlock(String userId, String complainCategory, String complainSub,
      Function(bool success, String msg) onResult) {
    var map = <String, String>{};
    if (userId.isNotEmpty) {
      map['broadcasterId'] = userId;
    }
    if (complainCategory.isNotEmpty) {
      map['complainCategory'] = complainCategory;
    }
    if (complainSub.isNotEmpty) {
      map['complainSub'] = complainSub;
    }

    network.reportOrBlock(map).then((result) {
      if (result.isSuccess) {
        if (complainCategory == "Report") {
          BtLogPvManger.stat(page: BtLogPages.report);
        } else if (complainCategory == "Block") {
          eventBus.fire(BtEventFn(block, obj: userId));
          unfriend(userId, (success, msg) => null);
          BtRcImEngine().removeConversation(userId);
          BtLogPvManger.stat(page: BtLogPages.block);
        }
      }
      onResult(
          result.isSuccess,
          result.isSuccess
              ? (complainCategory == "Report" ? 'Report successful' : 'Blocked successful')
              : result.msg);
    }).catchError((error) {
      // non-200 error goes here.
      switch (error.runtimeType) {
        case DioException:
          // Here's the sample to get the failed response error code and message
          final res = (error as DioException).response;
          logger.e('Got error : ${res?.statusCode} -> ${res?.statusMessage}');
          break;
        default:
          logger.e('Error occurred: $error');
          break;
      }
    });
  }

  Future<BtUserInfo?> getUserInfoById(String userId) async {
    var map = <String, String>{};
    map['userId'] = userId;
    BtApiResult<BtUserInfo>? result = await network.getUserInfo(map);
    if (result.isSuccess) {
      return result.data;
    }
    return null;
  }
}
